package com.shopping.service.impl;

import com.shopping.mapper.UserMapper;
import com.shopping.pojo.Browse;
import com.shopping.pojo.Cart;
import com.shopping.pojo.User;
import com.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //顾客登录
    @Override
    public User Userlogin(User user) {
        return userMapper.getByUsernameByPassword(user);
    }

    //    销售登录
    @Override
    public User Adminlogin(User user) {
        return userMapper.getByAdminnameByPassword(user);
    }

    //    顾客注册
    @Override
    public boolean UserRegister(User user) {
        User u = userMapper.searchByUsername(user.getUsername());
        if (u != null) {
            return false;
        }
        user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
        userMapper.insert(user);
        return true;
    }

    //    验证用户密码
    @Override
    public Integer authenticate(String username, String password) {
        User user = userMapper.searchByUsername(username);

        if (user != null && BCrypt.checkpw(password, user.getPassword())) {
            return user.getId(); // 验证通过
        }

        return 0; // 用户不存在或密码错误
    }

    //    验证管理员密码
    @Override
    public Integer Adminauthenticate(String username, String password) {
        User user = userMapper.searchByAdminname(username);

        if (user != null && BCrypt.checkpw(password, user.getPassword())) {
            return user.getId(); // 验证通过
        }

        return 0; // 用户不存在或密码错误
    }

    @Override
    public String getUserName(Integer customerId) {
        return userMapper.getUserName(customerId);
    }
    //注销账号
    @Override
    public void deleteUser(Integer customerId) {
        userMapper.deleteUser(customerId);
    }

    //获取用户列表
    @Override
    public List<User> getUserList() {
        List<User> list=userMapper.getUSerList();
        for(User user:list){

        }
        return list;
    }

    @Override
    public List<Browse> getBrowseLog(Integer id) {
        return userMapper.getBrowseLog(id);
    }

    @Override
    public List<Cart> getOrderLog(Integer id) {
        return userMapper.getOrderLog(id);
    }
}
